 var path = require('path');

 var fs = require('fs');


 var ajax = require('./ajax');

 module.exports = function (UC, source, config, ArFor, cache, package) {

     var source = config.Source;
     var UserControl = UC;
     var openjson = config.openjson;


     var router = function (req, res, next) {

         if (UserControl.getoff(req.session.key)) {
             var Request = req.body;

             Request = UserControl.GetForm(req.session.key, Request);
             if (Request["sourceid"] != null && UserControl.Source(req.session.key, Request["sourceid"])) {
                 ajax.GET('https://japp.oss-cn-shanghai.aliyuncs.com/PlugIn/index.json', {}, function (value, error) {

                     if (error) {
                         value = '{"Table":[]}';
                     }
                     value = JSON.parse(value);

                     /* Request = Object.assign({
                          page: 1,
                          size: 300,
                          sourceid: "M_670",
                          search: ""
                      }, Request);
                      */
                     new source(Request.sourceid, Request, function (obj) {

                         var Table = [];
                         var osstb = value.Table;
                         if (osstb.length > 0) {
                             var list = {}; 
                             if (obj && obj.Table) {
                                 var tb = obj.Table;
                                 for (var i = 0; i < tb.length; i++) {
                                     var element = tb[i];

                                     var PluginPath = path.join(config.Files.Plugin, element.localkey);
                                     var packagefile = path.join(PluginPath, 'package.json');
                                     if (fs.existsSync(packagefile)) {
                                         var version = require(packagefile).version;
                                         if (version) {
                                             version = parseInt(version.split('.').join(''))
                                             element.version = version;
                                         }
                                     };
                                     (delete element.localkey);
                                     list[element.appid] = element; 
                                 }
                             };
                             for (var i = 0; i < osstb.length; i++) {
                                 var element = osstb[i];
                                 if (!list[element.appid]) {
                                     element.state = 0;
                                     element.id = element.appid;
                                     config.PluginPool.set(element.appid, element);
                                     Table.push(element)
                                 } else if (list[element.appid] && element.version.split('.').join('') > list[element.appid].version) {
                                     (delete list[element.appid]);
                                     element.state = 6;
                                     element.id = element.appid;
                                     config.PluginPool.set(element.appid, element);
                                     Table.push(element)
                                 }
                             }
                             for(var key in list){
                                var element = list[key];
                                Table.push(element)
                             } 
                         } else if (obj && obj.Table) {
                             var table = obj.Table;
                             var _table = [];
                             for (var i = 0; i < table.length; i++) {
                                 var element = table[i];
                                 (delete element.localkey);
                                 _table.push(element);
                             };
                             Table = _table;
                         }
                         res.Json({
                             Table: Table
                         });
                     })
                 })
             } else {

                 var ajaxTest = {
                     error: 2
                 };
                 res.jsonp(ajaxTest);
             }
         } else {
             var ajaxTest = {
                 error: 1
             };
             res.jsonp(ajaxTest);

         }
     };

     return router;

 }